package com.qch.rabbitmq.demo.controller;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;

/**
 * @Author qch
 * @Date 2021/5/18
 */
@RestController
public class DelayedController {
    @Autowired
    AmqpTemplate amqpTemplate;
    @RequestMapping("/book/{seconds}")
    public String bookMeeting(@PathVariable Integer seconds) throws UnsupportedEncodingException {


        MessageProperties properties=MessagePropertiesBuilder.newInstance()
                .setHeader("x-delay",(seconds-10)*1000)
                .setContentEncoding("utf-8")
                .build();
        Message message = MessageBuilder.withBody("还有10秒开始开会了".getBytes("utf-8"))
                .andProperties(properties).build();
        amqpTemplate.send("ex.delayed","key.delayed",message);
        return "会议定好了";
    }
}
